<?php

/**
* 新闻公告
*
* @package		Hooloo framework
* @author 		Bill
* @copyright 	Hooloo Co.,Ltd
* @version		1.0
* @release		2017.05.25
*/
defined('BASEPATH') OR exit('No direct script access allowed');

class News  extends Controller{
	public function __construct(){
		parent::__construct();
	}
	
	/**
	* 新闻公告列表
	* Type:1,公告，2：新闻，3：交易规则
	*/
	public function index($type = 0, $title = 0, $p = 1) {
		//公告类型
		$type = (int)$type < 0 ? 0 : (int)$type;
		if($type){
			$where = " where type = '$type'";
		}else{
			$type = 0;
			$where = " where (type = 1 OR type = 2)";
		}
		
		//标题搜索
		$title = sql_format($title);
		if($title){
			$where .= " AND title like '%$title%'";
		} else {
			$title = 0;
		}
	
		//数据分页
		$p = (int)$p <= 0 ? 1 : (int)$p;
		$pize = 15;
		$start = ($p - 1) * $pize;
		
		//统计总数
		$sql = "select count(*) as c from news" . $where;
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];

		//分页查询
		$sql = "select *  from news" . $where . " order by id DESC limit $start, $pize";
		$list = $this->db->query($sql)->result_array();
		
		//分页展示 
		$url = "/" . $this->_controller . "/" . $this->_action . "/" . $type  . "/" . $title . "/";
		$page = page_format($total, $pize, $p, $url);
		
		//分配展示
		$this->assign("list", $list);
		$this->assign("page", $page);
		$this->assign("total", $total);
		$this->assign("type", $type);
		$this->assign("title", $title);

		$this->display();
	}
	
	/**
	* 交易规则
	* @param    text  	$content   	内容
	*/
	public function rule($type = 3){
		$sql = "select * from news where type = $type";
		$result = $this->db->query($sql)->row_array();
		$this->assign("result", $result);
		$this->display();
	}
}
